﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.IO;
using eDoc.BO;

namespace eDoc.Controls.App
{
    public partial class ucEditApp : System.Web.UI.UserControl
    {
        private int ID = 0;
        string urlRef="/";
        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (Request.QueryString["id"] != null && Request.QueryString["id"].ToString() != "")
            {
                ID = Convert.ToInt32(Request.QueryString["id"]);
            }
            else
                Response.Redirect("/listapp.aspx");

            if (!IsPostBack)
            {
                urlRef = Request.UrlReferrer.AbsolutePath;
                ViewState["urlRefe"] = urlRef;
                BindData();
            }
        }


        private void BindData()
        {
            DataTable dtAppMode = new DataTable();
            dtAppMode = BO.Data.Select_AppMode();
            ddlAppMode.DataValueField = "ID";
            ddlAppMode.DataTextField = "Mode_Name";
            ddlAppMode.DataSource = dtAppMode;
            ddlAppMode.DataBind();

            DataTable dt = new DataTable();
            dt = BO.Data.Select_AppPlist_by_ID(ID);
            if (dt != null && dt.Rows.Count > 0)
            {
                txtAppBundleIdentifier.Text = dt.Rows[0]["AppBundleIdentifier"].ToString();
                txtAppCFBundleSignature.Text = dt.Rows[0]["AppCFBundleSignature"].ToString();
                txtAppDeviceFamily.Text = dt.Rows[0]["AppDeviceFamily"].ToString();
                //txtAppIcon.Text = dt.Rows[0]["AppIcon"].ToString();
                txtAppKey.Text = dt.Rows[0]["AppKey"].ToString();
                txtAppMinimumOSVersion.Text = dt.Rows[0]["AppMinimumOSVersion"].ToString();
                txtAppName.Text = dt.Rows[0]["AppName"].ToString();
                txtAppDisplayName.Text = dt.Rows[0]["AppDisplayName"].ToString();
                txtAppPlistPath.Text = dt.Rows[0]["AppPlistPath"].ToString();
                txtAppSize.Text = dt.Rows[0]["AppSize"].ToString();
                txtAppUrlInstall.Text = dt.Rows[0]["AppUrlInstall"].ToString();
                txtAppVersion.Text = dt.Rows[0]["AppVersion"].ToString();
                txtAppDesc.Text = dt.Rows[0]["AppDesc"].ToString();
                chkPublished.Checked = Convert.ToBoolean(dt.Rows[0]["Published"]);
                chkJB.Checked = Convert.ToBoolean(dt.Rows[0]["AppJB"]);
                if (dt.Rows[0]["AppMode"].ToString() != "")
                    ddlAppMode.SelectedValue = dt.Rows[0]["AppMode"].ToString();
                if(dt.Rows[0]["AppIcon"].ToString()!="")
                {
                    imgAppIcon.ImageUrl = dt.Rows[0]["AppIcon"].ToString();
                }
                else
                {
                    imgAppIcon.ImageUrl = "/images/apple-02.png";

                }
            }

            
        }

        protected void btEditApp_Click(object sender, EventArgs e)
        {
            try
            {
                BO.Data.Update_AppPlist(ID, txtAppKey.Text.Trim(), txtAppName.Text.Trim(), txtAppDisplayName.Text.Trim(), txtAppVersion.Text.Trim(), txtAppBundleIdentifier.Text.Trim(), txtAppDeviceFamily.Text.Trim(), txtAppMinimumOSVersion.Text.Trim(), Convert.ToInt32(txtAppSize.Text.Trim()), txtAppPlistPath.Text.Trim(), txtAppUrlInstall.Text.Trim(), txtAppCFBundleSignature.Text.Trim(), imgAppIcon.ImageUrl, Convert.ToBoolean(chkPublished.Checked), Convert.ToInt32(ddlAppMode.SelectedValue), txtAppDesc.Text, Convert.ToBoolean(chkJB.Checked));
                if (ViewState["urlRefe"] != null)
                    urlRef = ViewState["urlRefe"].ToString();
                Response.Redirect(urlRef);
            }
            catch (Exception ex)
            {
                Global.log.Error("[EDIT APP] :: [ERROR] :: " + ex.Message + "\n\r" + ex.StackTrace);
                lblNote.Text = "Có lỗi xẩy ra. Không thực hiện được thao tác!";
            }
        }

        protected void btDelApp_Click(object sender, EventArgs e)
        {
            try
            {
                string urlInstall = txtAppUrlInstall.Text.Trim();
                string urlInfo = txtAppPlistPath.Text.Trim();
                string urlPlist = urlInfo.Replace("FileInfo","FilePList");

                /* dong lai cai
                DeleteFile(Server.MapPath(urlInstall));
                DeleteFile(Server.MapPath(urlPlist));
                DeleteFile(Server.MapPath(urlInfo));

                string[] arrInstall = urlInstall.Split("/".ToCharArray());
                string folderName = "";
                if (arrInstall.Length > 0)
                {
                    folderName = arrInstall[arrInstall.Length - 2].ToLower();
                }

                if (folderName != "")
                {
                    string folderInstall = Server.MapPath(Common.folderIPA) + folderName;
                    string folderInfo = Server.MapPath(Common.folderInfo) + folderName;
                    string folderPlist = Server.MapPath(Common.folderPlist) + folderName;

                    DeleteFolder(folderInstall);
                    DeleteFolder(folderInfo);
                    DeleteFolder(folderPlist);
                }
                */
                //DeleteFolder();

                BO.Data.Delete_AppPlist_by_ID(ID);

                ///File/FileInfo/EFBSystem_1.3/EFBSystem_1.3_4.plist



                if (ViewState["urlRefe"] != null)
                    urlRef = ViewState["urlRefe"].ToString();
                Response.Redirect(urlRef);
            }
            catch (Exception ex)
            {
                Global.log.Error("[DELETE APP] :: [ERROR] :: " + ex.Message + "\n\r" + ex.StackTrace);
                lblNote.Text = "Không thực hiện được thao tác!";
            }
        }

        private void DeleteFolder(string pathFolder)
        {
            if (Directory.Exists(pathFolder))
            {
                DirectoryInfo directory = new DirectoryInfo(pathFolder);
                List<FileInfo> fileInfos = directory.EnumerateFiles("*.*", SearchOption.AllDirectories).ToList();
                foreach (FileInfo f in fileInfos)
                    File.Delete(f.FullName);
                Directory.Delete(pathFolder);
            }
        }

        private void DeleteFile(string path)
        {
            if (File.Exists(path))
                File.Delete(path);
        }

        protected void btBack_Click(object sender, EventArgs e)
        {
            if (ViewState["urlRefe"] != null)
                urlRef = ViewState["urlRefe"].ToString();
            Response.Redirect(urlRef);
        }
    }
}